home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / PBLIB1 / UNITS / MISCPARS.INC < prev    next >
Text File  |  1994-02-17  |  12KB  |  463 lines

  1.  
  2. const Numerics      = [ '0'..'9','.','+','-' ];
  3. const Alphas        = [ 'A'..'Z','a'..'z' ];
  4. const AlphaNumerics = Alphas + Numerics;
  5.  
  6.  
  7.  
  8. {SECTION  CleanUpBlanks }
  9. Procedure CleanUpBlanks(var s : string);
  10. var done : boolean;
  11.     stringhold : string;
  12.      begin
  13.      Trim(s);
  14.      TokenizeStrings(s,stringhold);
  15.      RemoveExcessBlanks(s);
  16.      if stringhold > '' then DeTokenizeStrings(s,stringhold);
  17.      end;
  18.  
  19.  
  20. {SECTION  CleanUpComments }
  21. Procedure CleanUpComments(var s : string);
  22. { get rid of excess blanks to prepare for parsing }
  23. var done : boolean;
  24.      begin
  25.      RemoveEOLComments(s,commenteolchar);
  26.      done := false;
  27.      while not done do
  28.           done := RemoveBracketComments(s,commentpairLchar,commentpairRchar);
  29.      end;
  30.  
  31.  
  32. {SECTION  CleanUpString }
  33. Procedure CleanUpString(var s : string);
  34. { clean out comments and excess blanks to prepare for parsing }
  35.      begin
  36.      CleanUpComments(s);
  37.      CleanUpBlanks(s);
  38.      end;
  39.  
  40.  
  41. {SECTION DeQuoteString  }
  42. Function DeQuoteString(s : string) : string;
  43. var s1 : string;
  44.      begin
  45.      s1 := s;
  46.      if s1[1] = quotechar then delete(s1,1,1);
  47.      if s1[length(s1)] = quotechar then delete(s1,length(s1),1);
  48.      DeQuoteString := s1;
  49.      end;
  50.  
  51.  
  52. {SECTION  DeTokenizeStrings }
  53. Procedure DeTokenizeStrings(var s,hold : string);
  54. var i,j : integer;
  55.     dummy : boolean;
  56.     s2  : string;
  57.      begin
  58.      i := 1;
  59.      while i > 0 do
  60.          begin
  61.          i := pos(qstringtoken,s);
  62.          if i > 0 then
  63.               begin
  64.               s2 := '';
  65.              { s[i] := '#'; }
  66.               delete(s,i,1);
  67.               dummy := ReplaceStringWithToken(hold,s2,qstringtoken);
  68.               insert(s2,s,i);
  69.               s2 := '';
  70.               end;
  71.          end;
  72.      end;
  73.  
  74.  
  75. {SECTION  GETAlphaNumericStr }
  76. Function  GETAlphaNumericStr ( var s : string) : string;
  77. var s1 : string;
  78.     i  : integer;
  79.      begin
  80.      s1 := '';
  81.      while (length(s) > 0) and not (s[1] in AlphaNumerics) do delete(s,1,1);
  82.      while (length(s) > 0) and     (s[1] in AlphaNumerics) do
  83.           begin
  84.           s1 := s1 + s[1];
  85.           delete(s,1,1);
  86.           end;
  87.      GETAlphaNumericStr := s1;
  88.      end;
  89.  
  90.  
  91. {SECTION  GETAlphaStr }
  92. Function  GETAlphaStr ( var s : string) : string;
  93. var s1 : string;
  94.     i  : integer;
  95.      begin
  96.      s1 := '';
  97.      while (length(s) > 0) and not (s[1] in Alphas) do delete(s,1,1);
  98.      while (length(s) > 0) and     (s[1] in Alphas ) do
  99.           begin
  100.           s1 := s1 + s[1];
  101.           delete(s,1,1);
  102.           end;
  103.      GETAlphaStr := s1;
  104.      end;
  105.  
  106.  
  107.  
  108. {SECTION  GETBoolean }
  109. Function  GETBoolean (var s : string) : boolean;
  110. var x : boolean;
  111.     s1 : string;
  112.     code : integer;
  113.      begin
  114.      x := true;
  115.      s1 := UpCaseStr(GetAlphaStr(s));
  116.      if (s1 = 'NO') or (s1 = 'OFF') then x := false;
  117.      GETBoolean := x;
  118.      end;
  119.  
  120.  
  121. {SECTION  GetDelimitedStr }
  122. Function  GETDelimitedStr ( var s : string; lchr,rchr : char) : string;
  123.              {[STRING] Removes string in paired brackets, l & r CAN be same}
  124. var s1   : string;
  125.     i,j,l  : integer;
  126.      begin
  127.      s1 := '';
  128.      i := lscan(s,lchr);
  129.      if (i > 0) then
  130.           begin
  131.           j := rscan(s,rchr);
  132.           if j > i then
  133.                begin
  134.                l := (j - i) - 1;
  135.                if (j > i) then
  136.                     begin
  137.                     if (l > 0) then s1 := copy(s,i+1,l);
  138.                     delete(s,i,(j-i+1));
  139.                     end;
  140.                end;
  141.           end;
  142.      GETDelimitedStr := trimstr(s1);
  143.      end;
  144.  
  145.  
  146.  
  147.  
  148.  
  149. {SECTION  GETInteger }
  150. Function  GETInteger (var s : string) : integer;
  151. var x : integer;
  152.     s1 : string;
  153.     code : integer;
  154.      begin
  155.      x := 0;
  156.      s1 := GetNumericStr(s);
  157.      val(s1,x,code);
  158.      GETInteger := x;
  159.      end;
  160.  
  161.  
  162. {SECTION  GetLeftStr }
  163. Function  GetLeftStr ( var s : string; tch : char) : string;
  164. { Note, if char not there, returns WHOLE string }
  165. var s1 : string;
  166.     i,l     : integer;
  167.      begin
  168.      trim(s);
  169.      i := pos(tch,s);
  170.      if i > 0 then
  171.           begin
  172.           s1 := copy(s,1,i-1);
  173.           delete(s,1,i);
  174.           end
  175.      else begin
  176.           s1 := s;
  177.           s := '';
  178.           end;
  179.      GetLeftStr := trimstr(s1);
  180.      end;
  181.  
  182.  
  183. {SECTION  GETLongInt }
  184. Function  GETLongInt (var s : string) : longint;
  185. var x : longint;
  186.     s1 : string;
  187.     code : integer;
  188.      begin
  189.      x := 0;
  190.      s1 := GetNumericStr(s);
  191.      val(s1,x,code);
  192.      GETLongInt := x;
  193.      end;
  194.  
  195.  
  196. {SECTION  GETNumericStr }
  197. Function  GETNumericStr ( var s : string) : string;
  198. var s1 : string;
  199.     i  : integer;
  200.      begin
  201.      s1 := '';
  202.      while (length(s) > 0) and not (s[1] in Numerics) do delete(s,1,1);
  203.      while (length(s) > 0) and     (s[1] in Numerics) do
  204.           begin
  205.           s1 := s1 + s[1];
  206.           delete(s,1,1);
  207.           end;
  208.      GETNumericStr := s1;
  209.      end;
  210.  
  211.  
  212. {SECTION  GETReal }
  213. Function  GETReal (var s : string) : real;
  214. var x : real;
  215.     s1 : string;
  216.     code : integer;
  217.      begin
  218.      x := 0;
  219.      s1 := GetNumericStr(s);
  220.      val(s1,x,code);
  221.      GETReal := x;
  222.      end;
  223.  
  224.  
  225. {SECTION  GetRightStr }
  226. Function  GetRightStr ( var s : string; tch : char) : string;
  227. { Note, if char not there, returns EMPTY string }
  228. var s1 : string;
  229.     i,l     : integer;
  230.      begin
  231.      s1 := trimstr(s);
  232.      i := rscan(s1,tch);
  233.      if i > 0 then
  234.           begin
  235.           s := copy(s1,1,i-1);
  236.           delete(s1,1,i);
  237.           end
  238.      else begin
  239.           s1 := '';
  240.           end;
  241.      GetRightStr := trimstr(s1);
  242.      end;
  243.  
  244.  
  245.  
  246.  
  247. {SECTION LScan  }
  248. Function LScan(str : string; tch : char) : byte;
  249.       {[STRING] finds FIRST occurance of char TCH in string STR }
  250. var i,j : integer;
  251.      begin
  252.      j := 0;
  253.      i := 0;
  254.      while (i < length(str)) and (j = 0) do
  255.           begin
  256.           inc(i);
  257.           if str[i] = tch then j := i;
  258.           end;
  259.      LScan := j;
  260.      end;
  261.  
  262.  
  263. {SECTION  NibbleString  }
  264. Function  NibbleString(var s : string;tch : termchars; var termch : char) : string;
  265.            {[STRING] fetches to one of a SET of chars - see also GetLeftStr }
  266. var dummy, done : boolean;
  267.     i     : integer;
  268.     stringhold,s1 : string;
  269.      begin
  270.      termch := '%';
  271.      s1 := '';
  272.      RemoveLeading(s,' ');
  273.      TokenizeStrings(s,stringhold);
  274.      if s[1] = qstringtoken then
  275.           begin
  276.           dummy := ReplaceStringWithToken(stringhold,s1,qstringtoken);
  277.           delete(s,1,1);
  278.           delete(stringhold,1,1);
  279.           termch := ' ';
  280.           end
  281.      else begin
  282.           done := false;
  283.           i := 1;
  284.           while (i <= length(s)) and not done do
  285.                begin
  286.                if (s[i] in tch) then
  287.                     begin
  288.                     s1 := copy(s,1,i-1);
  289.                     termch := s[i];
  290.                     delete(s,1,i);
  291.                     done := true;
  292.                     end
  293.                else inc(i);
  294.                end;
  295.           if not done then
  296.                begin
  297.                s1 := s;
  298.                s := '';
  299.                end;
  300.           end;
  301.      if stringhold > '' then
  302.           DeTokenizeStrings(s,stringhold);
  303.      NibbleString := s1;
  304.      end;
  305.  
  306.  
  307.  
  308. {SECTION  RemoveBracketComments }
  309. Function  RemoveBracketComments(var s : string; lchar,rchar : char) : boolean;
  310. { get rid of comments to prepare for parsing }
  311. var i,j,k : integer;
  312.     done  : boolean;
  313.      begin
  314.      done := true;
  315.      if lchar <> chr(0) then
  316.          begin
  317.          if multilinecomment then
  318.               begin  {looking for close}
  319.               j := pos(rchar,s);
  320.               if j > 0 then
  321.                    begin
  322.                    delete(s,1,j);
  323.                    multilinecomment := false;
  324.                    done := false;
  325.                    end
  326.               else s := '';   {still in multiline comment}
  327.               end
  328.          else begin  {looking for open comment }
  329.               i := pos(lchar,s);
  330.               if i > 0 then
  331.                    begin
  332.                    done := false;
  333.                    j := pos(rchar,s);
  334.                    if j > i then
  335.                         begin
  336.                         delete(s,i,(j-i)+1);
  337.                         end
  338.                    else begin
  339.                         s := leftstr(s,i-1);
  340.                         multilinecomment := true;
  341.                         end;
  342.                    end;
  343.               end;
  344.          end;
  345.      RemoveBracketComments := done;
  346.      end;
  347.  
  348.  
  349. {SECTION  RemoveDelimitedString }
  350. Procedure RemoveDelimitedString ( var s : string; lchr,rchr : char);
  351. var s1 : string[1];
  352.      begin
  353.      s1 := GetDelimitedStr(s,lchr,rchr);
  354.      end;
  355.  
  356.  
  357.  
  358. {SECTION  RemoveEOLComments }
  359. Procedure RemoveEOLComments(var s : string; cchar : char);
  360. { get rid of comments to prepare for parsing }
  361. var i : integer;
  362.      begin
  363.      if cchar <> chr(0) then
  364.          begin
  365.          i := pos(cchar,s);
  366.          if i > 0 then
  367.               begin
  368.               s := leftstr(s,i-1);
  369.               end;
  370.          end;
  371.      end;
  372.  
  373.  
  374. {SECTION  ReplaceStringWithToken }
  375. Function  ReplaceStringWithToken(var s,s1 : string; token : char) : boolean;
  376. var notdone : boolean;
  377.     i,j     : integer;
  378.      begin
  379.      notdone := false;
  380.      s1 := '';
  381.      i := pos(quotechar,s);
  382.      if i > 0 then
  383.           begin
  384.           s[i] := token;
  385.           j := pos(quotechar,s);
  386.           if j > i then
  387.               begin
  388.               s1 := quotechar + copy(s,i+1,(j-i));
  389.               delete(s,i+1,(j-i));
  390.               notdone := true;
  391.               end
  392.           else s[i] := quotechar;   { mismatched quotes, put it back }
  393.           if s[i] = chr(0) then delete(s,i,1);
  394.           end;
  395.      ReplaceStringWithToken := notdone;
  396.      end;
  397.  
  398.  
  399. {SECTION Rpos  }
  400. Function Rpos(substr,str : string) : byte;
  401.       {[STRING] equivalent to pos, but returns last occurance }
  402. var i,j : integer;
  403.     s : string;
  404.      begin
  405.      j := 0; i := 0;
  406.      s := str;
  407.      i := pos(substr,s);
  408.      while i > 0 do
  409.           begin
  410.           j := i;      { j will have the position of the last match }
  411.           s[i] := '~'; { so doesn't match again }
  412.           i := pos(substr,s);
  413.           end;
  414.      Rpos := j;
  415.      end;
  416.  
  417.  
  418.  
  419.  
  420. {SECTION RScan  }
  421. Function RScan(str : string; tch : char) : byte;
  422.       {[STRING] finds LAST occurance of char TCH in string STR }
  423. var i,j : integer;
  424.      begin
  425.      j := 0;
  426.      for i := 1 to length(str) do if str[i] = tch then j := i;
  427.      RScan := j;
  428.      end;
  429.  
  430.  
  431. {SECTION  ScanStufInit }
  432. Procedure ScanStufInit;
  433.      begin
  434.      quotechar        := chr(34);   { double quote char }
  435.      commenteolchar   := chr(33);   { exclamation point }
  436.      commentpairLchar := chr(123);  { left squiggley bracket }
  437.      commentpairRchar := chr(125);  { right squiggley bracket }
  438.      qstringtoken     := chr(255);  { something unnatural in a string }
  439.      multilinecomment := false;     {true while mismatched comment brackets}
  440.      end;
  441.  
  442.  
  443. {SECTION  ShiftUPString }
  444. Procedure ShiftUPString(var s : string);
  445. var done : boolean;
  446.     stringhold : string;
  447.      begin
  448.      TokenizeStrings(s,stringhold);
  449.      s := UpCaseStr(s);
  450.      if stringhold > '' then DeTokenizeStrings(s,stringhold);
  451.      end;
  452.  
  453.  
  454. {SECTION  TokenizeStrings }
  455. Procedure TokenizeStrings(var s,hold : string);
  456. var i,j : integer;
  457.     s1  : string;
  458.      begin
  459.      hold := '';
  460.      while ReplaceStringWithToken(s,s1,qstringtoken) do hold := hold + s1;
  461.      end;
  462.  
  463.